package com.szj.pet.hospital.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.szj.pet.hospital.mapper.ReserveMapper;
import com.szj.pet.hospital.model.cmn.Dict;
import com.szj.pet.hospital.model.hosp.Reserve;
import com.szj.pet.hospital.service.ReserveService;
import com.szj.pet.hospital.vo.cmn.DictEeVo;
import com.szj.pet.hospital.vo.hosp.ReserveEeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class ReserveServiceImpl extends ServiceImpl<ReserveMapper, Reserve> implements ReserveService {
    //导出数据字典接口
    @Override
    public void exportReserveData(HttpServletResponse response) {
        //设置下载信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = "reserve";
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //查询数据库
        List<Reserve> reserveList = baseMapper.selectList(null);
        //Dict -- DictEeVo
        List<ReserveEeVo> reserveEeVoList = new ArrayList<>();
        for (Reserve reserve : reserveList) {
            ReserveEeVo reserveEeVo = new ReserveEeVo();
            // dictEeVo.setId(dict.getId());
            BeanUtils.copyProperties(reserve, reserveEeVo);
            reserveEeVoList.add(reserveEeVo);
        }
        //调用方法进行写操作
        try {
            EasyExcel.write(response.getOutputStream(), ReserveEeVo.class).sheet("reserve")
                    .doWrite(reserveEeVoList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
